package com.qf.service.impl;

import com.qf.dao.AccountDao;
import com.qf.pojo.Account;
import com.qf.service.AccountService;
import com.qf.util.TransactionManager;
import org.apache.commons.dbutils.QueryRunner;

/**
 * @ClassName AccountServiceImpl
 * @Description TODO
 * @Author 86181
 * @Date 2022/8/31 15:12
 * @Version 1.0
 **/
public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao){
        this.accountDao=accountDao;
    }
    private TransactionManager transactionManager;
    public void setTransactionManager(TransactionManager transactionManager){
        this.transactionManager=transactionManager;
    }
    public void transfer(String sourceName, String targetName, Double money) {
        transactionManager.begin();
        Account sourceAccount = accountDao.findAccountByName(sourceName);
        //根据名称查询转入账用户
        Account targetAccount = accountDao.findAccountByName(targetName);
        //转出账户扣钱
        sourceAccount.setMoney(sourceAccount.getMoney() - money);
        //转入账户加钱
        targetAccount.setMoney(targetAccount.getMoney() + money);
        //更新转出账户
        accountDao.updateAccount(sourceAccount);
        int i=1/0;
        //更新转入账户
        accountDao.updateAccount(targetAccount);
        //提交事务


    }
}
